import java.lang.reflect.Proxy;

/**
 * 计算器单元测试类
 * 使用单元测试方式测试代理服务器的校验功能
 */
public class CalculatorUnitTest {
    private Calculator proxy;
    private YiXinZhiProxyHandler handler;

    /**
     * 初始化测试环境
     */
    public void setUp() {
        Calculator calculator = new CalculatorImpl();
        handler = new YiXinZhiProxyHandler(calculator);
        proxy = (Calculator) Proxy.newProxyInstance(
                CalculatorImpl.class.getClassLoader(),
                CalculatorImpl.class.getInterfaces(),
                handler);
    }

    /**
     * 测试用例1：参数包含负数，应该返回-1
     */
    public void testAddWithNegativeParameter() {
        setUp();
        int result = proxy.add(10, -5);
        assert result == -1 : "测试失败：参数包含负数时应返回-1，实际返回: " + result;
        System.out.println("✓ 测试通过：参数包含负数时返回-1");
    }

    /**
     * 测试用例2：结果超过int最大值，应该返回-2
     */
    public void testAddWithOverflow() {
        setUp();
        int result = proxy.add(1111111111, 2000000000);
        assert result == -2 : "测试失败：结果溢出时应返回-2，实际返回: " + result;
        System.out.println("✓ 测试通过：结果溢出时返回-2");
    }

    /**
     * 测试用例3：正常计算，应该返回正确结果
     */
    public void testAddNormal() {
        setUp();
        int result = proxy.add(20241167, 1429);
        assert result == 20242596 : "测试失败：正常计算应返回20242596，实际返回: " + result;
        System.out.println("✓ 测试通过：正常计算返回正确结果");
    }

    /**
     * 测试用例4：边界值测试 - 两个0相加
     */
    public void testAddZero() {
        setUp();
        int result = proxy.add(0, 0);
        assert result == 0 : "测试失败：0+0应返回0，实际返回: " + result;
        System.out.println("✓ 测试通过：0+0返回0");
    }

    /**
     * 测试用例5：边界值测试 - 最大int值加0
     */
    public void testAddMaxInt() {
        setUp();
        int result = proxy.add(Integer.MAX_VALUE, 0);
        assert result == Integer.MAX_VALUE : "测试失败：MAX_VALUE+0应返回MAX_VALUE，实际返回: " + result;
        System.out.println("✓ 测试通过：MAX_VALUE+0返回MAX_VALUE");
    }

    /**
     * 测试用例6：边界值测试 - 最大int值加1（溢出）
     */
    public void testAddMaxIntPlusOne() {
        setUp();
        int result = proxy.add(Integer.MAX_VALUE, 1);
        assert result == -2 : "测试失败：MAX_VALUE+1应返回-2（溢出），实际返回: " + result;
        System.out.println("✓ 测试通过：MAX_VALUE+1返回-2（溢出）");
    }

    /**
     * 测试用例7：统计信息功能测试
     */
    public void testStatistics() {
        setUp();
        proxy.add(10, 20);
        proxy.add(-5, 10);
        proxy.add(1111111111, 2000000000);
        proxy.add(5, 5);

        int callCount = handler.getCallCount();
        int failureCount = handler.getValidationFailureCount();

        assert callCount == 4 : "测试失败：调用次数应为4，实际: " + callCount;
        assert failureCount == 2 : "测试失败：失败次数应为2，实际: " + failureCount;

        System.out.println("✓ 测试通过：统计信息正确");
        System.out.println("   " + handler.getStatistics());
    }

    /**
     * 运行所有测试
     */
    public static void main(String[] args) {
        System.out.println("========== 单元测试开始 ==========\n");

        CalculatorUnitTest test = new CalculatorUnitTest();
        int passCount = 0;
        int failCount = 0;

        try {
            test.testAddWithNegativeParameter();
            passCount++;
        } catch (AssertionError e) {
            failCount++;
            System.out.println("✗ 测试失败：" + e.getMessage());
        }

        try {
            test.testAddWithOverflow();
            passCount++;
        } catch (AssertionError e) {
            failCount++;
            System.out.println("✗ 测试失败：" + e.getMessage());
        }

        try {
            test.testAddNormal();
            passCount++;
        } catch (AssertionError e) {
            failCount++;
            System.out.println("✗ 测试失败：" + e.getMessage());
        }

        try {
            test.testAddZero();
            passCount++;
        } catch (AssertionError e) {
            failCount++;
            System.out.println("✗ 测试失败：" + e.getMessage());
        }

        try {
            test.testAddMaxInt();
            passCount++;
        } catch (AssertionError e) {
            failCount++;
            System.out.println("✗ 测试失败：" + e.getMessage());
        }

        try {
            test.testAddMaxIntPlusOne();
            passCount++;
        } catch (AssertionError e) {
            failCount++;
            System.out.println("✗ 测试失败：" + e.getMessage());
        }

        try {
            test.testStatistics();
            passCount++;
        } catch (AssertionError e) {
            failCount++;
            System.out.println("✗ 测试失败：" + e.getMessage());
        }

        System.out.println("\n========== 单元测试结果 ==========");
        System.out.println("总测试数: " + (passCount + failCount));
        System.out.println("通过: " + passCount);
        System.out.println("失败: " + failCount);
        System.out.println("成功率: " + String.format("%.2f%%", (double) passCount / (passCount + failCount) * 100));
    }
}
